define([ "dojo/_base/declare", "widgets/baseWidget/BaseWidget",
    "dojo/text!./templates/Template.html","dojo/json", "dojo/_base/array",
    "dojo/dom-class", "esri/symbols/PictureMarkerSymbol","dojo/on","esri/request",
    "esri/geometry/Polyline","esri/graphic","esri/symbols/SimpleLineSymbol","esri/geometry/Point",
    "esri/geometry/Extent","dojo/_base/Color","esri/SpatialReference","dojo/parser","esri/map",
    "esri/layers/ArcGISTiledMapServiceLayer","dojo/topic",

        "esri/symbols/SimpleFillSymbol",
        "esri/layers/FeatureLayer",

    "dojo/on",
    "esri/fx","esri/dijit/Scalebar",  "esri/layers/ArcGISDynamicMapServiceLayer","esri/renderers/SimpleRenderer"
    ,"esri/config"
    ],
    function(declare,BaseWidget, template,json, array, domClass,PictureMarkerSymbol,on,request,Polyline,
    		Graphic,SimpleLineSymbol,Point,Extent,Color,SpatialReference,Parser,Map,ArcGISTiledMapServiceLayer,
    		Topic,SimpleFillSymbol,FeatureLayer, on,FX,Scalebar,ArcGISDynamicMapServiceLayer,SimpleRenderer,esriConfig) {
        return declare([ BaseWidget], {
        	//模板页面
            templateString : template,
            //地图实例
            map:null,

            constructor : function() {
            	this.inherited(arguments);
                Global.arrNodeFeaLyer = [];  // 用来存储treeId 和 featureLayer

            },
            //组件初始化方法
            postCreate : function() {
            	this.inherited(arguments);
            	//创建地图
            	var initExt = new Extent(ioc.map.initExt);
                var labels = ioc.map.labels;
                this.map = new Map(this.mapcon, {
                    extent: initExt,
                    slider: false,
                    sliderStyle: "large",
                    sliderLabels: labels,
                    logo:false});
                var temp=this;
                this.map.on("load",function(){  // 数据加载完毕后
                	 var scalebar = new Scalebar({  
                         map: temp.map, // 必须的  
                         scalebarUnit:"metric",
                         scalebarStyle:"line"
                       });  
                	Global.mapLoaded=true;  // 地图加载完毕了
                    Topic.publish("map_loaded");
                });
                this.initLayers();
                on(this,"Click",function(e){
                	Topic.publish('mapClick',e.mapPoint);
                });
            },
            initLayers:function(){   // 初始化地图方法   在上面调用了
            	esriConfig.defaults.io.proxyUrl = "proxy.jsp";
            	var mapservices = ioc.map.basemaps;
            	Global.cnLayer=new ArcGISTiledMapServiceLayer(mapservices.cn);  // basemap切片图
                this.map.addLayer(Global.cnLayer);

                  // 之前的使用数据库存储的  发布的featureLayerservice的情况

//                // 初始化featureLayer的rest  Global 可以使得地图在全局可以被访问
//                Global.urlStrFt = "http://localhost:6080/arcgis/rest/services/JTworks/chinamapFeaturelayer/MapServer";
//                Global.MapcolorFeature = new Color([255,0,0]);
//                Global.defaultSymbolCH = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,Global.MapcolorFeature,3);
//                Global.MapchRenderer = new SimpleRenderer(Global.defaultSymbolCH);
//
//                for(var i = 0; i< 4; i++){
//                    Global.arrNodeFeaLyer[i] = new FeatureLayer(Global.urlStrFt+"/"+i);
//                    Global.arrNodeFeaLyer[i].setRenderer(Global.MapchRenderer);
//                    this.map.addLayer( Global.arrNodeFeaLyer[i]);
//                    Global.arrNodeFeaLyer[i].visible = true;
//                    // 最后隐藏各个图, 需要添加  要循环这个存储数组来添加
//                }

            },
            startup:function(){
            },
            resizeHandle:function(){
            	this.map.reposition();
            	this.map.resize();
            },
            destroy:function(){
            	this.inherited(arguments);
            }

        });
    });
